package com.hzhk.happyrent.ui.activity.personal;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.view.ClearEditText;
import com.hzhk.happyrent.utils.MyPerference;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PwdInputActivity extends AppCompatActivity {

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.act_user_pwd)
    ClearEditText actUserPwd;
    @BindView(R.id.act_login_submit)
    Button actLoginSubmit;
    @BindView(R.id.iv_see_pwd)
    ImageView ivSeePwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pwd_input);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        activityTitlebarCenter.setText(R.string.password_change);
        ivSeePwd.setTag(false);
    }

    @OnClick({R.id.activity_titlebar_left, R.id.act_login_submit,R.id.iv_see_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.act_login_submit:
                initData();
                break;
            case R.id.iv_see_pwd:
                transformation();
                break;
        }
    }

    private void transformation() {
        boolean tag = (Boolean) ivSeePwd.getTag();
        if (tag) {
            ivSeePwd.setImageResource(R.drawable.pwd_disvisible);
            ivSeePwd.setTag(false);
            actUserPwd
                    .setTransformationMethod(PasswordTransformationMethod
                            .getInstance());
            actUserPwd.setSelection(actUserPwd.getText()
                    .toString().trim().length());
        } else {
            ivSeePwd.setImageResource(R.drawable.pwd_visible);
            ivSeePwd.setTag(true);
            actUserPwd
                    .setTransformationMethod(HideReturnsTransformationMethod
                            .getInstance());
            actUserPwd.setSelection(actUserPwd.getText()
                    .toString().trim().length());
        }
    }

    private void initData() {
        if (TextUtils.isEmpty(actUserPwd.getText().toString())) {
            Toast.makeText(PwdInputActivity.this, "请输入新密码", Toast.LENGTH_SHORT).show();
            return;
        } else if (actUserPwd.getText().toString().length() < 6) {
            Toast.makeText(this, R.string.tv_pwd_rule, Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("id", MyPerference.getInstance().getUserId());
        params.put("phone", MyPerference.getInstance().getUserPhone());
        params.put("code", getIntent().getStringExtra("code"));
        params.put("password", actUserPwd.getText().toString());
        HttpManager.getInstance().perform_post(API.EDIT_USER_PWD, params, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {
                Toast.makeText(PwdInputActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }

            @Override
            public void onNext(Object tag, String response) {
                CodeModel codeModel = new Gson().fromJson(response, CodeModel.class);
                if (0 == codeModel.getErrCode()) {
                    Toast.makeText(PwdInputActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK, getIntent());
                    finish();
                }
            }
        });
    }

    @OnClick(R.id.iv_see_pwd)
    public void onViewClicked() {
    }
}
